
#include <REGX51.H>
#define uchar unsigned char
#define uint unsigned int
uchar hour = 7, min = 59, sec = 55;
uchar code tab[] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90 };
sbit fmq = P3^6;
sbit K1 = P0^0;
sbit K2 = P0^1;
sbit K3 = P0^2;
sbit K4 = P0^3;
sbit K5 = P0^4;
sbit K6 = P0^5;

void delay(uint i)
{
	uchar j;
	while (i--)
		for (j = 0; j < 125; j++);
}

void display()
{
	P2 = 0x01;
	P1 = tab[hour / 10];
	delay(1);

	P2 = 0x02;
	P1 = tab[hour % 10];
	delay(1);

	P2 = 0x04;
	P1 = 0xbf;
	delay(1);

	P2 = 0x08;
	P1 = tab[min / 10];
	delay(1);

	P2 = 0x10;
	P1 = tab[min % 10];
	delay(1);

	P2 = 0x20;
	P1 = 0xbf;
	delay(1);
	
	P2 = 0x40;
	P1 = tab[sec / 10];
	delay(1);

	P2 = 0x80;
	P1 = tab[sec % 10];
	delay(1);
}

void Keyscan()
{
	if (K1 == 0)
	{
		delay(150);
		if (K1 == 0)
		{
			hour = hour + 1;
			if (hour == 24)
				hour = 0;
		}
	}
	if (K2 == 0)
	{
		delay(150);
		if (K2 == 0)
		{
			hour = hour - 1;
			if (hour == -1)
				hour = 23;
		}
	}
	if (K3 == 0)
	{
		delay(150);
		if (K3==0)
		{
			min = min + 1;
			if (min == 60)
				min = 0;
		}
	}
	if (K4 == 0)
	{
		delay(150);
		if (K4 == 0)
		{
			min = min - 1;
			if (min == -1)
				min = 59;
		}
	}
	if (K5 == 0)
	{
		delay(150);
		if (K5 == 0)
		{
			sec = sec + 1;
			if (sec == 60)
				sec = 0;
		}
	}
	if (K6 == 0)
	{
		delay(150);
		if (K6 == 0)
		{
			sec = sec - 1;
			if (sec == -1)
			{
				sec = 59;
			}
		}
	}
}

void main()
{
	EA = 1;
	EX1 = 1;
	IT1 = 1;

	TMOD = 0x01;
	TH0 = (65536 - 1000) / 256;
	TL0 = (65536 - 1000) % 256;
	TR0 = 1;
	ET0 = 1;

	TMOD = 0x10;
	TH1 = (65536 - 50000) / 256;
	TL1 = (65536 - 50000) % 256;
	TR1 = 1;
	ET1 = 1;

	while (1)
	{
		display();
		Keyscan();
	}
}

void JS(void) interrupt 3
{
	uint t;
	t = t + 1;
	if (t == 20)
	{
		sec++;
		if (sec == 60)
		{
			sec = 0;
			min++;
			if (min == 60)
			{
				min = 0;
				hour++;
				if (hour == 24)
				{
					hour = 0;
				}
			}
		}
		t = 0;
	}
	TH1 = (65536 - 50000) / 256;
	TL1 = (65536 - 50000) % 256;
}

void int1(void) interrupt 2
{
	EX1 = 0;
	hour = 7; min = 29; sec = 57;
	delay(500);
	EX1 = 1;
}
void Time(void) interrupt 1
{
	if (hour == 8 && min == 0 && sec == 0)
		fmq = ~fmq;
	TH0 = (65536 - 1000) / 256;
	TL0 = (65536 - 1000) % 256;
}